package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.bxd.baodan.R;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.Utils;

/**
 * 注册设置密码
 *
 * @author syq
 */
public class FindPwdActivity extends BaseActivity implements OnClickListener {
    private EditText etInputPwd, etInputName;
    private Button btnEye;
    private Spinner spSex;
    private String phone;
    private String vcode;
    private Boolean canSeePassword = false;
    private int sex = -1;
    private Button ok_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_pwd);


    }

    private void initView() {
//        etInputPwd = getEditText(R.params.et_input_pwd);
//        etInputName = getEditText(R.params.et_input_name);
//        btnEye = getButton(R.params.eye_open_or_clause);
//        spSex = getSpinner(R.params.sp_sex_choice);
//        ok_btn = getButton(R.params.ok_btn);

        btnEye.setOnClickListener(this);
        ok_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
//            case R.params.eye_open_or_clause:
//                canSeePassword = !canSeePassword;
//                if (canSeePassword) {
//                    // 显示密码
//                    btnEye.setBackgroundResource(R.drawable.see_pwd);
//                    etInputPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//                } else {
//                    // 隐藏密码
//                    btnEye.setBackgroundResource(R.drawable.eyeclosed);
//                    etInputPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//                }
//                break;
//            case R.params.ok_btn:
//                //根据注册结果进行跳转
//                RegistResult();
//                break;

            default:
                break;
        }
    }

    /**
     * 请求注册方法
     */
    private void RegistResult() {
        if (TextUtils.isEmpty(getString(etInputPwd))) {
            etInputPwd.setError("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(getString(etInputName))) {
            etInputName.setError("请输入用户名");
            return;
        }
        if ("男".equals(getString(spSex))) {
            //男
            sex = 1;
        } else if ("女".equals(getString(spSex))) {
            sex = 2;
        }
        NetUtil netUtil = new NetUtil(FindPwdActivity.this, "注册资料");
        netUtil.setParams("phone", phone);
        netUtil.setParams("vcode", vcode);
        netUtil.setParams("password", getString(etInputPwd));
        netUtil.setParams("name", getString(etInputName));
        netUtil.setParams("sex", sex);
        netUtil.setParams("channelid", Utils.getAppMetaData(this, "UMENG_CHANNEL"));
        netUtil.postRequest("提交中...", new RequestStringListener() {

            @Override
            public void onComplete(String result) {
                // TODO Auto-generated method stub
                if (JSONUtil.isSuccess(result)) {
                    LoginInfo.getInstance(getApplicationContext()).saveLoginInfo(phone, getString(etInputPwd));
                    LoginInfo.refresh(FindPwdActivity.this);//刷新登录资料
                    Intent intent = new Intent(FindPwdActivity.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in_right,
                            R.anim.slide_out_left);
                } else {
                    showToast(JSONUtil.getMessage(result));
                }
            }
        });

    }
    public String getString(Spinner sp) {
        try {
            return sp.getSelectedItem().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
